
import Toast from '@vant/weapp/toast/toast';
import {clearUserLocalStorage} from "../../../../utils/util";
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        view_type: 0,
        view_text: '[全天]',
        name:'',
        summary_list:[],

        view_delivery_man_id: 0,
        view_driver_name: '',
        view_delivery_district_id: 0,

				is_print: false,
				activeNames: ['0'], //点击折叠

        formattedContent: ''
    },
/**
     * 返回上一页
     */
    onClickLeft() {
			navigateBack()
	},
    formatQuantity(quantity) {
        if (Number.isInteger(quantity)) {
            return quantity.toString();
        } else {
            return quantity.toFixed(2);
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */

    async  onLoad(options) {
        let order_no = options.order_no;
        let now = (new Date().getTime()) / 1000;
        let expiration_time = wx.getStorageSync('expiration_time')
        let group = wx.getStorageSync('role')

        let current_delivery_man_id = wx.getStorageSync('current_delivery_man_id')
        if (!current_delivery_man_id || current_delivery_man_id == 0) {
            current_delivery_man_id = 0
        }

        if (group !== '配送司机') {
            clearUserLocalStorage()
            expiration_time = 0
        }

        let access_token = wx.getStorageSync('access_token')
        let delivery_man = wx.getStorageSync('delivery_man')
        let delivery_district = wx.getStorageSync('delivery_district')
        let delivery_district_list = wx.getStorageSync('delivery_district_list')
        let delivery_list = wx.getStorageSync('delivery_list')
        this.setData({
            'access_token': access_token,
            'delivery_man': delivery_man,
            'delivery_district': delivery_district,
            'delivery_district_list': delivery_district_list,
            'delivery_list': delivery_list,
            'current_delivery_district_id': 0,
            'level': delivery_man.level,
            'current_delivery_man_id': current_delivery_man_id,
            order_no
        })

        let view_delivery_man_id = wx.getStorageSync('view_delivery_man_id') || 0
        let view_driver_name = ''
        let view_delivery_district_id = 0
        if (view_delivery_man_id > 0) {
            view_driver_name = wx.getStorageSync('view_driver_name')
            view_delivery_district_id = wx.getStorageSync('view_delivery_district_id')
        }

        this.setData({
            view_delivery_man_id,
            view_driver_name,
            view_delivery_district_id
        })

        this.getOrderGoods(0)
    },
/**
     * 默认打开折叠
     */
    onChange(event){
			this.setData({
					activeNames: event.detail,
				});
	},
    async printOrder() {
        this.getOrderGoods(1)
    },
    //根据订单id查询对应的订单信息
    async  getOrderGoods(print_type = -1) {
        let delivery_man = wx.getStorageSync('delivery_man');
        let delivery_man_id = delivery_man.delivery_man_id
        let order_no = this.data.order_no
        let view_delivery_man_id = wx.getStorageSync('view_delivery_man_id') || 0
        let view_driver_name = ''
        let view_delivery_district_id = 0
        if (view_delivery_man_id > 0) {
            view_driver_name = wx.getStorageSync('view_driver_name')
            view_delivery_district_id = wx.getStorageSync('view_delivery_district_id')
        }

        let that  = this;
        wx.p.request({
            url: app.globalData.API + '/delivery/index/get-order-goods',
            header: {
                'Content-Type': 'application/json',
                "x-api-key": this.data.access_token,
            },
            method: "POST",
            data:{
                delivery_man_id: delivery_man_id,
                order_no,
                print_type
            },

            success(res){
                let order = res.data.data.order
                order.detailList.forEach(item => {
                    // 判断goods_no属性的前缀
                    if (item.attr_no.startsWith('GH')) {
                        // 如果以GH开头，则添加干货标签，标签颜色为黄色
                        item.tag = '干货';
                        item.tagColor = 'warning';
                    } else if (item.attr_no.startsWith('LC')) {
                        // 如果以LC开头，则添加冷藏标签，标签颜色为蓝色
                        item.tag = '冷藏';
                        item.tagColor = 'primary';
                    }
                });

                let content = res.data.data.content
                let is_print = false
                let print_msg = res.data.data.print_msg
                if (print_msg !== undefined) {
                    Toast(print_msg);
                }
                let formattedContent = undefined
                console.log(content)
                if (content!==undefined) {
                    formattedContent = content[0].split('<BR>').map(line => {
                        return `<div style="text-align: left;">${line
                            .replace(/<CB>(.*?)<\/CB>/g, '<div class="cb">$1</div>')
                            .replace(/<B>(.*?)<\/B>/g, '<span class="b">$1</span>')
                            .replace(/<C>(.*?)<\/C>/g, '<div class="c">$1</div>')
                            .replace(/<L>(.*?)<\/L>/g, '<span class="l">$1</span>')
                            .replace(/<W>(.*?)<\/W>/g, '<span class="w">$1</span>')
                            .replace(/<QR>(.*?)<\/QR>/g, '<div class="qr">$1</div>')
                            .replace(/<RIGHT>(.*?)<\/RIGHT>/g, '<div class="right">$1</div>')
                            .replace(/<BOLD>(.*?)<\/BOLD>/g, '<span class="bold">$1</span>')
                            .replace(/<BC128_A>(.*?)<\/BC128_A>/g, '<div class="barcode128a">$1</div>')
                            .replace(/<BC128_B>(.*?)<\/BC128_B>/g, '<div class="barcode128b">$1</div>')
                            .replace(/<BC128_C>(.*?)<\/BC128_C>/g, '<div class="barcode128c">$1</div>')
                        }</div>`;
                    });
                }

                that.setData({
                    order: order,
                    is_print: is_print,
                    formattedContent
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})